// import Hybrid from '@/components/uzone-hybrid';
import UploadImage from '@/components/upload-image';
// import ubt from '@/service/ubt';
import MixinValidateForm from '@/service/mixin-validate-form';
import NavBar from '../components/navBar1';
import doFaceButton from '@/components/doFaceButton';

export default {
    name: 'indetification-information',
    components: {
        NavBar,
        UploadImage,
        doFaceButton
    },
    mixins: [MixinValidateForm],
    data() {
        return {
            isShowFace:false,
            showUpFront:true,
            productId: '',
            readonly: false,
            identificationType: '',
            form: {
                cardPositiveImg: '',
                // faceValid: 0, // 识别状态
            },
            // payImgReadonly: false,
            // workerImgReadonly: false,
            // orcImgReadonly: false,
            // owwaImgReadonly: false,
            // tinImgReadonly: false,
            // driverImgReadonly: false,
            // philHealthImgReadonly: false,
            // pagibigImgReadonly: false,
            // businessLicenseImgReadonly: false,
            status: {
                identity: null,
                personal: null,
                work: null,
                contact: null,
                additional: null,
            },
            btnName: this.$t('next'),
            disabledBtn: false,
            isUploadNewImg: false,
            isReviewPassed: false, // 订单是否审核通过
            showMoreImg: false, // 显示更多图片上传
            lockInfo: true, // 是否锁定身份信息
            pageTrackerInfo: {
                page: 'authInfo',
                startTime: new Date().getTime()
            }
        };
    },
    computed: {
        btnCanSubmit() {
            // const { cardPositiveImg, cardImg ,payImg, workerImg, orcImg, owwaImg, tinImg, driverImg, philHealthImg, pagibigImg, businessLicenseImg, } = this.form;
            // const data = [payImg, workerImg, orcImg, owwaImg, tinImg, driverImg, philHealthImg, pagibigImg, businessLicenseImg];
            // const arr = [];
            // data.forEach(item => {
            //     if (item && item !== '') {
            //         arr.push(item);
            //     }
            // });
            // return arr.length < 1 || !cardPositiveImg || !cardImg;

            const { cardPositiveImg } = this.form;
            return !cardPositiveImg;
        },
        nameLabel() {
            const type = this.identificationType;
            switch (type) {
                case 'UMID (Unified Multi-Purpose ID)':
                    return 'UMID';
                case 'TIN ID (Taxpayer Identification Number)':
                    return 'TIN ID';
                case 'SSS ID (Social Security System)':
                    return 'SSS ID';
                case 'PRC ID (Professional Regulation Commission)':
                    return 'PRC ID';
                case 'OWWA ID (Overseas Workers Welfare Administration)':
                    return 'OWWA ID';
                default:
                    return type;
            }
        },
    },
    methods: {
        /**
         * 获取数据
         */
        getData() {
            this.$api.checkFaceVaild().then(res => {
                const { status, error, data } = res;
                if(status=='0'&&error=='02000130'){
                    this.isShowFace = true;
                    this.goFaceCheck();
                }else{
                    this.showUpFront = true;
                    this.isShowFace = false;
                }
            });
        },
        /**
         * 获取资料填写度
         */
        getInfoData() {
            this.$api.getDataIntegrity().then(res => {
                const { status, error, data } = res;
                if ('1' === status && ('00000000' === error || '0000' === error) && data) {
                    const status = this.status;
                    status.identity = (data.USERBASEINFO === '1'); // 基本信息
                    status.personal = (data.USERPERSONALINFO === '1'); // 个人信息
                    status.work = (data.USERJOB === '1'); // 工作信息
                    status.contact = (data.USERCONTACT === '1'); // 联系人信息
                    status.additional = (data.USERCARDID === '1'); // 身份信息
                    if(data.USERCARDID === '1'){
                        this.readonly = 0
                    }
                }
            });
        },
        /**
         * 跳转填资
         */
        goInformation(url) {
            const path = '/information/' + url;
            this.$router.push({
                path,
                query: { productId: this.productId }
            });
        },
        /**
         * 表单提交
         */
        submitHandler() {
            if (this.disabledBtn) return
            // 认证信息页 - 提交 - 点击
            this.$buryPoint.action({
                eventName: 'authInfo_submit_click',
                ...this.pageTrackerInfo
            });

            let form = this.form;
            if(this.isShowFace){
                this.goFaceCheck();
                return
            };
            if(!form.cardPositiveImg||form.cardPositiveImg==''){
                this.$toast('Please complete your information');
                return
            };
            const params = {
                'cardPositiveImg': form.cardPositiveImg,
            };
            this.disabledBtn = true;
            this.$api.addOrUpdateUserCardId(params).then(res => {
                const { status, error, msg ,data} = res;
                if ('1' === status && ('00000000' === error || '0000' === error)) {
                    if(data.repeatFlag==0){
                        this.disabledBtn = false
                        this.$toast(this.$t('todayNumberExhausted'));
                        return
                    }
                    if(data.status==2){
                        this.disabledBtn = false
                        this.$toast(this.$t('successAdd'));
                        return
                    };
                    if(data.status==3){
                        this.disabledBtn = false
                        this.isShowFace = true;
                        this.goFaceCheck();
                        return
                    }
                    if(data.status==4){
                        this.disabledBtn = false
                        this.$toast(this.$t('plsUpcorrectClearPhont'));
                        return
                    }

                     // 认证信息页 - 提交 - 成功
                     this.$buryPoint.action({
                        eventName: 'authInfo_submit_success',
                        ...this.pageTrackerInfo
                    });
                    this.$buryPoint.action({
                        eventName: 'authInfo_face_compare_success',
                        ...this.pageTrackerInfo
                    });
                    this.$refs.checkFaceInfo.returnTo().catch(() => {
                        this.disabledBtn = false
                    });
                } else {
                    this.disabledBtn = false
                    this.$toast(msg);
                }
            }).catch(error => {
                this.disabledBtn = false;
            });
        },
        // appShowMain() {
        //     // const hybrid = new Hybrid(this.$os.android ? 'android' : 'ios');
        //     this.$hybrid.action('showMain', {
        //         param: { bool: false },
        //         callback(data) {}
        //     });
        // },
        /**
         * 人脸识别
         */
        goFaceCheck() {
            if(!this.isShowFace) return

           // 认证信息页人脸认证点击
           this.$buryPoint.action({
                eventName: 'authInfo_face_click',
                ...this.pageTrackerInfo
            });

            // const hybrid = new Hybrid(this.$os.android ? 'android' : 'ios');
            const me = this;
            this.$hybrid.action('discern', {
                param: {
                    count:1,
                },
                callback(data) {
                    if (data.status === 1) {
                        if (data.data.status === 1) {
                            me.isShowFace = false;
                            me.showUpFront = true;

                            // // 认证信息页 - 人脸认证 - 成功
                            // me.$hybrid.action('ee', {
                            //     content: 'authInfo_face_success',
                            //     value: '',
                            // });
                            me.$buryPoint.action({
                                eventName: 'authInfo_face_success',
                                ...me.pageTrackerInfo
                            });

                        }else{
                            me.goFaceCheck()
                        }
                        // if (data.data.status === 0) {
                        // }
                    }
                }
            });
        },
        uploadNewImg() {
            this.isUploadNewImg = true;
        },
        queryUserCardId(){
            this.$api.queryUserCardId().then(res=>{
                const { status, error ,data} = res;
                if(status=='1'&&'00000000' === error){
                    this.form.cardPositiveImg = data.cardPositiveImg;
                }
            })
        },

        /**
         * 获取当前订单状态
         */
        // checkOrder(type) {
        //     if (this.readonly) {
        //         this.$api.getLastOrderByCode().then(res => {
        //             const { status, error, data } = res;
        //             if ('1' === status && ('00000000' === error || '0000' === error)) {
        //                 if (type === 'status') {
        //                     this.isReviewPassed = (data.status === '2' || data.status === '4' || data.status === '5' || data.status === '7' || data.status === '8' || data.status === '10');
        //                 }
        //                 if (type === 'submit') {
        //                     const isCansubmit = !((data.status === '2' || data.status === '4' || data.status === '5' || data.status === '7' || data.status === '8' || data.status === '10'));
        //                     if (!isCansubmit) {
        //                         this.$toast('You can\'t revise information in the process of loan');
        //                     } else {
        //                         this.submitHandler();
        //                     }
        //                 }
        //             }
        //         });
        //     }else if (!this.readonly && type === 'submit') {
        //         this.submitHandler();
        //     }
        // }
    },
    created() {
        // this.appShowMain();
        const query = this.$route.query;
        this.productId = query.productId;
        if (query.btnType && query.btnType === 'save') { // 按钮是下一步，还是保存
            this.btnName = this.$t('save');
        }
        this.getInfoData();
        this.getData();
        this.queryUserCardId();
    },
    beforeDestroy () {
        this.pageEndAction()
    },
    mounted () {
        this.pageStartAction()
        // 认证信息页 - 页面 - 曝光
        this.$buryPoint.action({
            eventName: 'authInfo_page_show',
            ...this.pageTrackerInfo
        });
    }
};